// pages/report/report.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    serviceDesc: '',
    orderId: null,
    imageSrc: [], // 初始显示预设图片
    imageNum: 0,
    location: "选择当前位置",
    detailPics: [],
    lat: "",
    lng: ""
  },





  /**
   * 获取文本内容
   */
  text: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  address: function () {
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        wx.chooseLocation({
          success: function (res) {
            console.log(res)
            console.log(res.address);
            if (res.name == res.address) {
              that.setData({
                location: res.address,
                lat: res.latitude,
                lng: res.longitude
              })

            } else {
              that.setData({
                location: res.address + "(" + res.name + ")",
                lat: res.latitude,
                lng: res.longitude
              });
            }
          }
        })
      }
    })
  },
  // 预览图片
  preview(e) {
    var that = this;
    var index = e.currentTarget.dataset.index
    wx.previewImage({
      current: that.data.detailPics[index],
      urls: that.data.detailPics,

    })
  },


  // },

  uploadDetailImage: function (e) { //这里是选取图片的方法
    var that = this;
    var pics = [];
    var picList = [];
    var detailPics = that.data.detailPics;
    if (detailPics.length >= 9) {
      wx.showToast({
        title: '最多选择' + 9 + '张！',
        icon: "none"
      })
      return;
    }

    wx.chooseImage({
      count: 9 - this.data.detailPics.length, // 最多可以选择的图片张数，默认9
      sizeType: ['compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        var imgs = res.tempFilePaths;
        console.log(imgs);
        console.log(res);
        // for (var i = 0; i < imgs.length; i++) {
        //   pics.push(imgs[i])
        // }
        console.log(pics)
        // if (res.tempFileSize < 5000000) {
        console.log(imgs);
        pics = imgs;
        for (var i = 0; i < pics.length; i++) {
          if (res.tempFiles[i].size > 5000000) {
            wx.showToast({
              title: '图片上传失败',
              icon: "none"
            })
          }
        }

        console.log(pics);
        // } else {
        //   wx.showToast({
        //     title: '图片上传失败',
        //     icon: "none"
        //   })
        // }
        // console.log(that.data.detailPics);
        console.log(that.data.lat);
        console.log(that.data.lng);
        for (let i = 0; i < pics.length; i++) {
          console.log(pics[i]);
          wx.uploadFile({
            url: 'https://www.xiaozheng8.com/upload',
            // url: 'https://www.xiaozheng8.com/upload',
            filePath: pics[i],
            name: 'file',
            header: {
              'Content-Type': 'multipart/form-data'
            },
            formData:{
              lat: that.data.lat,
              lng: that.data.lng
            },
            success: function (res) {
              console.log(res);
              that.data.detailPics.push(res.data);
              console.log(res.data);
              that.setData({
                detailPics: that.data.detailPics
              })
              if (that.data.detailPics.length > 9) {
                wx.showToast({
                  title: '最多选择' + 9 + '张！',
                  icon: "none"
                })
                that.data.detailPics.splice(9, that.data.detailPics.length - 9)
                that.setData({
                  detailPics: that.data.detailPics
                })
              }
              console.log(that.data.detailPics)

            },fail(res){
              console.log(res);
              wx.showToast({
                title: '图片上传失败',
                icon:"none"
              })
            }

          })

        }

        console.log(that.data.detailPics)

      },
    })

  },

  cancel: function (e) {
    var index = e.currentTarget.dataset.index;
    console.log(index);
    this.data.detailPics.splice(index, 1);
    this.setData({
      detailPics: this.data.detailPics
    })
  },
  //多张图片上传
  // uploadimg: function (data) {
  // wx.showLoading({
  //   title: '上传中...',
  //   mask: true,
  // })
  // var that = this,
  //   i = data.i ? data.i : 0,
  //   success = data.success ? data.success : 0,
  //   fail = data.fail ? data.fail : 0;
  // wx.uploadFile({
  //   url: data.url,
  //   filePath: data.path[i],
  //   name: 'fileData',
  //   formData: null,
  //   success: (resp) => {
  //     wx.hideLoading();
  //     success++;
  //     var str = resp.data //返回的结果，可能不同项目结果不一样
  //     var pic = JSON.parse(str);
  //     var pic_name = that.data.showUrl + pic.Data;
  //     var detailPics = that.data.detailPics;
  //     detailPics.push(pic_name)
  //     that.setData({
  //       detailPics: detailPics
  //     })
  //   },
  //   fail: (res) => {
  //     fail++;
  //     console.log('fail:' + i + "fail:" + fail);
  //   },
  //   complete: () => {
  //     i++;
  //     if (i == data.path.length) { //当图片传完时，停止调用     
  //       console.log('执行完毕');
  //       // console.log('成功：' + success + " 失败：" + fail);
  //       var myEventDetail = {
  //         picsList: that.data.detailPics
  //       } // detail对象，提供给事件监听函数
  //       var myEventOption = {} // 触发事件的选项
  //       that.triggerEvent('myevent', myEventDetail, myEventOption)//结果返回调用的页面
  //     } else { //若图片还没有传完，则继续调用函数
  //       data.i = i;
  //       data.success = success;
  //       data.fail = fail;
  //       that.uploadimg(data);//递归，回调自己
  //     }
  //   }
  // });
  // },


  /**
   * 提交报告
   */
  submit: function () {
    let that = this;
    console.log(that.data.imageSrc);
    console.log(that.data.detailPics)
    if (that.data.location == "选择当前位置") {
      wx.showModal({
        title: "提示",
        content: '请选择当前位置',
        showCancel: false,
        confirmText: '确认',
        confirmColor: '#0bbd79',
      })
    } else if (that.data.detailPics == '') {
      wx.showModal({
        title: "提示",
        content: '请上传图片',
        showCancel: false,
        confirmText: '确认',
        confirmColor: '#0bbd79',
      })
    } else {
      wx.showModal({
        title: "提示",
        content: '我已上传施工现场图片',
        cancelText: '取消',
        cancelColor: '#999',
        confirmText: '确认',
        confirmColor: '#0bbd79',
        success: function (res) {
          if (res.confirm) {
            console.log(that.data.lat);
            console.log(that.data.lng);
            wx.request({
              url: 'https://www.xiaozheng8.com/serviceOrder/setStatus/v3',
              method: 'post',
              data: {
                status: that.data.status,
                orderId: that.data.orderId,
                userId: wx.getStorageSync("userId"),
                images: that.data.detailPics,
                address: that.data.location,
                lat: that.data.lat,
                lng: that.data.lng
              },
              success: function (res) {
                console.log(res);
                wx.navigateBack({

                })

              },
            })
          } else { }

        }
      })

    }

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      status: options.status,
      orderId: options.orderId
    });
    var that=this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success(res) {
        console.log(res);
        that.setData({
          lat:res.latitude,
          lng:res.longitude
        })
        const latitude = res.latitude
        const longitude = res.longitude
       
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})